package one.SnackGame;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class GameFrame extends JFrame{

    //游戏状态 0.未开始 1.游戏ing 2.暂停 3.失败 4.胜利 5.失败后重启
    public static int state = 0;
    //分数
    public int score = 0;
    //定义双缓存图片
    Image ScreenImage = null;
    //窗口宽高
    int width = 800;
    int height = 600;

    //蛇头对象
    SnackHead snackhead = new SnackHead(GameUtility.upImage, 60, 300, this);

    //蛇身集合
    public List<SnackBody> snackBodies = new ArrayList<>();
    //获取食物
    public SnackFood snackFood = new SnackFood().getFood();

    //打印窗口
    public void printFrame(){
        //窗口可见
        setVisible(true);
        //窗口大小
        setSize(width, height);
        //窗口居中
        setLocationRelativeTo(null);
        //窗口标题
        setTitle("Snack Game");

        //蛇身初始化
        snackBodies.add(new SnackBody(GameUtility.bodyImage, 30, 300, this));
        snackBodies.add(new SnackBody(GameUtility.bodyImage, 0, 300, this));

        //键盘操纵
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if(e.getKeyCode() == KeyEvent.VK_SPACE){
                    switch (state) {
                        //开始
                        case 0:
                            state = 1;
                            break;
                        //暂停
                        case 1:
                            state = 2;
                            break;
                        //继续
                        case 2:
                            state = 1;
                            break;
                        //失败后重新开始
                        case 3:
                            state = 5;
                        default:
                            break;
                    }
                }
            }
        });

        while(true){
            if(state == 1) {
                repaint();
            }
            if(state == 5){
                state = 0;
                reset();
            }
            try {
                //1秒等于1000毫秒
                Thread.sleep(200);
            }
            catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    }

    //画出网格
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //初始化双缓存图片
        if(ScreenImage == null){
            ScreenImage = this.createImage(width, height);
        }
        //获取双缓存画笔
        Graphics gImage = ScreenImage.getGraphics();
        //背景颜色
        gImage.setColor(Color.gray);
        gImage.fillRect(0, 0, width, height);
        //勾勒横线和竖线
        gImage.setColor(Color.black);
        for(int i = 0; i < 20; i++){
            gImage.drawLine(0, i * 30, 600, i * 30);
        }
        gImage.setColor(Color.black);
        for(int i = 0; i < 20; i++){
            gImage.drawLine(i * 30, 0, i * 30, 600);
        }
        //绘制蛇身
        for(int i = snackBodies.size()-1; i>=0; i--){
            snackBodies.get(i).paintSelf(gImage);
        }
        //绘制蛇头
        snackhead.paintSelf(gImage);
        //绘制食物
        snackFood.paintSelf(gImage);
        //绘制分数
        GameUtility.drawWorld(gImage,  score + "分", Color.BLUE, 50, 650, 300);
        //绘制提示语
        gImage.setColor(Color.gray);
        prompt(gImage);
        //将双缓存图片绘制到窗口
        g.drawImage(ScreenImage, 0, 0, null);
    }

    void prompt(Graphics g){
        //未开始
        if(state == 0){
            g.fillRect(120, 240, 400, 70);
            GameUtility.drawWorld(g, "按空格开始游戏", Color.YELLOW  , 35, 150, 290);
        }
        //暂停
        if(state == 2){
            g.fillRect(120, 240, 400, 70);
            GameUtility.drawWorld(g, "按空格继续游戏", Color.YELLOW, 35, 150, 290);
        }
        //失败
        if(state == 3){
            g.fillRect(120, 240, 400, 70);
            GameUtility.drawWorld(g, "游戏失败，按空格重新开始", Color.RED, 35, 150, 290);
        }
        //通关
        if(state == 4){
            g.fillRect(120, 240, 400, 70);
            GameUtility.drawWorld(g, "六百六十六", Color.GREEN, 35, 150, 290);
        }
    }
    //游戏重置
    void reset(){
        //关闭当前窗口
        this.dispose();
        //开启新窗口
        String[] args = {};
        main(args);
    }

    public static void main (String[] args) {
        GameFrame game = new GameFrame();
        game.printFrame();
    }
}
